// (C) 2022 Intel Corporation. All rights reserved.
// Your use of Intel Corporation's design tools, logic functions and other 
// software and tools, and its AMPP partner logic functions, and any output 
// files from any of the foregoing (including device programming or simulation 
// files), and any associated documentation or information are expressly subject 
// to the terms and conditions of the Intel Program License Subscription 
// Agreement, Intel FPGA IP License Agreement, or other applicable 
// license agreement, including, without limitation, that your use is for the 
// sole purpose of programming logic devices manufactured by Intel and sold by 
// Intel or its authorized distributors.  Please refer to the applicable 
// agreement for further details.

module RSU_TOP
    #(
        parameter FwRevision  = 16'h10CF, 
        parameter DeviceAddr  = 8'hAC,    
        parameter DeviceType  = 16'h11AD, 
        parameter SupportMode = 8'h05    
    )
(
    input          Clock,
    input          RuReset,
    input          Reset,
    input          LockPin,
	 input          I2C_CRC_Error,
    //expose flash ctrl&data avmm intf out
    output          avmm_csr_addr,          
    output          avmm_csr_read,            
    output [31: 0]  avmm_csr_writedata,        
    output          avmm_csr_write,            
    input  [31: 0]  avmm_csr_readdata,      
    output [31: 0]  avmm_data_addr,          
    output          avmm_data_read,            
    output [31: 0]  avmm_data_writedata,        
    output          avmm_data_write,            
    input  [31: 0]  avmm_data_readdata,           
    input           avmm_data_waitrequest,          
    input           avmm_data_readdatavalid,          
    output [ 6: 0]  avmm_data_burstcount,    
   //i2c_slave avmm intf
    input  [15: 0]  avmm_address,      //Write/Read Address for writing to registers
    input  [15: 0]  avmm_writedata,    //Write Data for writing to registers
    input           avmm_write,        //Write Enable for marking Address and Data as valid to write
    input           avmm_read, 
    output          avmm_readvalid,
    output [15: 0]  avmm_readdata      //Read Data for reading from registers    
);  

//Internal Wires
  wire            ReloadApplication;        //Force an FPGA reload of application image
  wire            ReloadApplication0;       //Force an FPGA reload of application image0
  wire            ApplicationError;         //Indicates an error occurred loading application image
  wire    [ 6: 0] FlashMemWrAddress;        //Write Address for writing to storage RAM for flash writes
  wire    [15: 0] FlashMemWrData;           //Write Data for writing to storage RAM for flash writes
  wire            FlashMemWrEn;             //Write Enable for marking Address and Data as valid to write
  wire    [31: 0] FlashOpAddr;              //Address for flash write operation
  wire    [ 5: 0] FlashOpLen;               //Number of 32-bit words to do in write operation
  wire            FlashOpWr;                //Starts a flash write if unlocked and ready
  wire            FlashOpEr;                //Starts a flash erase if unlocked and ready
  wire            FlashCmdAck;              //Acknowledges flash command to clear lock and command bits
  wire            FlashBusy;                //Flash operation is currently under way
  wire            FlashError;               //An error has occurred with the flash command
  wire    [ 3: 0] ConfigCurrent;    
  wire    [15: 0] FlashStatusVal;   
  

  //Instantiate module to control configuration
  MAX10_REMOTE_UPDATE Max10RuControl
    (
      .Clock               ( Clock              ),
      .RuReset             ( RuReset            ),
      .ReloadApplication   ( ReloadApplication  ),
      .ReloadApplication0  ( ReloadApplication0 ),
      .ApplicationError    ( ApplicationError   ),
      .ConfigCurrent       ( ConfigCurrent      )
    );
  
  //Instantiate module to control flash
  MAX10_FLASH_INTERFACE Max10FlashInterface
    (
      .Clock              ( Clock                   ),
      .Reset              ( Reset                   ),
      .FlashMemWrAddress  ( FlashMemWrAddress       ),
      .FlashMemWrData     ( FlashMemWrData          ),
      .FlashMemWrEn       ( FlashMemWrEn            ),
      .FlashOpAddr        ( FlashOpAddr             ),
      .FlashOpLen         ( FlashOpLen              ),
      .FlashOpWr          ( FlashOpWr               ),
      .FlashOpEr          ( FlashOpEr               ),
      .FlashCmdAck        ( FlashCmdAck             ),
      .FlashBusy          ( FlashBusy               ),
      .FlashError         ( FlashError              ),
      .FlashStatusVal     ( FlashStatusVal          ),
      //expose flash ctrl&data avmm intf out
      .CntlAddrReg        ( avmm_csr_addr           ),   
      .CntlRdReg          ( avmm_csr_read           ),     
      .CntlWrDataReg      ( avmm_csr_writedata      ), 
      .CntlWrReg          ( avmm_csr_write          ),     
      .CntlRdData         ( avmm_csr_readdata       ),    
      .DataAddrReg        ( avmm_data_addr          ),   
      .DataRdReg          ( avmm_data_read          ),     
      .DataWrDataReg      ( avmm_data_writedata     ), 
      .DataWrReg          ( avmm_data_write         ),     
      .DataRdData         ( avmm_data_readdata      ),    
      .DataWaitReq        ( avmm_data_waitrequest   ),   
      .DataRdValid        ( avmm_data_readdatavalid ),   
      .DataBurstCntReg    ( avmm_data_burstcount    )   
    );
    
  //Instantiate Register File
  RSU_CSR 
    #(
        .FwRevision   ( FwRevision  ),
        .DeviceAddr   ( DeviceAddr  ),
        .DeviceType   ( DeviceType  ),
        .SupportMode  ( SupportMode )
    )
  RSU_CSR_INST  
    (
      .Clock                        (Clock                        ),
      .Reset                        (Reset                        ),
      //avmm intf connect to I2C slave
      .avmm_address                 (avmm_address                 ),            
      .avmm_writedata               (avmm_writedata               ),          
      .avmm_write                   (avmm_write                   ),              
      .avmm_read                    (avmm_read                    ),               
      .avmm_readvalid               (avmm_readvalid               ),
      .avmm_readdata                (avmm_readdata                ),     
      //connect to internal flash
      .FlashMemWrAddress            (FlashMemWrAddress            ),
      .FlashMemWrData               (FlashMemWrData               ),
      .FlashMemWrEn                 (FlashMemWrEn                 ),
      .FlashOpAddr                  (FlashOpAddr                  ),
      .FlashOpLen                   (FlashOpLen                   ),
      .FlashOpWr                    (FlashOpWr                    ),
      .FlashOpEr                    (FlashOpEr                    ),
      .FlashCmdAck                  (FlashCmdAck                  ),
      .FlashBusy                    (FlashBusy                    ),
      .FlashError                   (FlashError                   ),
      .FlashStatusVal               (FlashStatusVal               ),
      //connect to higher level
      .LockPin                      (LockPin                      ),
      .I2C_CRC_Error                (I2C_CRC_Error                ),
      //connect to RSU HIP
      .ConfigCurrent                (ConfigCurrent                ),
      .ApplicationError             (ApplicationError             ),
      .ReloadApplication0           (ReloadApplication0           ),
      .ReloadApplication            (ReloadApplication            )
    );
    
endmodule
